<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/10/18
 * Time: 1:10 AM
 */

namespace app\api\controller\jiaoyi;


use think\Config;
use think\Controller;
use think\Db;
use think\Request;

class Huazhuan extends Controller
{

    private $zongqianbao = '0x50f1Ca5621496A700FB7042c77E3Fd6d5c62427A';//总钱包地址

    private $zongqianbaokey = '0xebd3d69c9461354a49283eab33bc082ff73499dc85a93606ec517d0308ff08cd';//总钱包私钥

    private $host_ip = '47.92.195.55';//钱包服务器IP

    private $gas = 0.005;//转出场外手续费

    /**
     * 钱包到交易所
     *
     * @uname  当前操作的用户
     *
     * @type  货币类型
     *
     * @pass  支付密码
     *
     *
     */
    public function Qianbaojiaoyi(){

        $res = Request::instance();

        $uname = $this->Jiemi($res->param('uname'));

        $pass = $this->Jiemi($res->param('pass'));

        $type = $this->Jiemi($res->param('type'));

        $value = $res->param('value');

        $user_pass = Db::name('user')->where('uname',$uname)->value('secpwd');

        $user_key = Db::name('adeth')->where('uname',$uname)->value('key');

        if (!$pass){echo json_encode(['zt'=>0,'msg'=>'支付密码不能为空!']);die();}

        if (md5($pass) !== $user_pass){echo json_encode(['zt'=>0,'msg'=>'密码错误']);die();}

        $user_address = Db::name('adeth')->where('uname',$uname)->value('address');

        $balance = Db::name('adeth')->where('uname',$uname)->value(strtolower($type));

        if (floatval($balance) < (0.0005+$value)){echo json_encode(['zt'=>0,'msg'=>'余额不足,账户最少要留0.0005以太坊']);die();}

        if ($value < 0.000000001){echo json_encode(['zt'=>0,'msg'=>'最小转入不能小于0.1']);die();}

        if (is_float($value)||is_int($value)){echo json_encode(['zt'=>0,'msg'=>'请输入正确的数量']);die();}



        if ($res->isPost()){

            if (strtolower($type) == 'eth'){

                $zhuan = file_get_contents('http://'.$this->host_ip.':8884/?type=3&from='.$user_address.'&privateKey='.$user_key.'&to='.$this->zongqianbao.'&value='.$value);

                if (!$zhuan){echo json_encode(['zt'=>0,'msg'=>'操作失败！']);die();}

                Db::startTrans();

                try{

                    Db::name('jiaoyiqianbaolog')->insert([

                        'uname' => $uname,

                        'hash' => json_decode($zhuan)->hash,

                        'jiaoyizt' => 2,

                        'jiaoyinum' => $value,

                        'jiaoyiinfo' => '场外转入',

                        'jiaoyitype' => 0,

                        'from' => $user_address,

                        'to' => $this->zongqianbao,

                        'bizhong' => strtolower($type),

                        'time' => time()

                    ]);
                    //Db::name('jiaoyinonce')->where('id',1)->setInc('nonce',1);

                    Db::commit();

                    echo json_encode(['zt'=>1,'msg'=>'广播成功']);

                } catch (\Exception $e) {

                    echo json_encode(['zt'=>0,'msg'=>'充值失败,稍后重试']);

                    Db::rollback();
                }


            }else{

                echo json_encode(['zt'=>0,'msg'=>'暂时只支持以太坊']);die();

                $daibizhuan = file_get_contents('http://'.$this->host_ip.':8885/?type=2&from='.$user_address.'&privateKey='.$user_key.'&to='.$this->zongqianbao.'&value='.$value.'&contract='.Config::get('con_' . strtolower($type)));

                //echo $daibizhuan;

                echo json_encode(['zt'=>1,'msg'=>$daibizhuan]);

            }



        }else{

            echo json_encode(['zt'=>0,'msg'=>'error']);

        }

    }
    //获取本地记录
    public function Get_local_info(){

        $res = Request::instance();

        $data = Db::name('jiaoyiqianbaolog')->where('uname',$res->param('uname'))->where('bizhong',$res->param('type'))->order('id desc')->select();

        if ($res){

            echo json_encode(['zt'=>1,'msg'=>$data]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'信息获取失败']);

        }
    }
    //判断交易哈希释放到账
    public function ifvhash(){

        $res = Request::instance();

        $data = Db::name('jiaoyiqianbaolog')->where('uname',$res->param('uname'))->where('bizhong',$res->param('type'))->order('id desc')->paginate(30);

        foreach ($data as $value){

            if ($value['jiaoyizt'] == 2){

                $zt = json_decode(file_get_contents('http://47.92.195.55:8887/?type=1&hash='.$value['hash']))->hash->status;

                if (strlen($zt) <= 4){

                    if ($value['jiaoyitype'] == 0){//转入增加

                        Db::startTrans();

                        try{

                            Db::name('jiaoyiqianbaolog')->where('hash',$value['hash'])->update(['jiaoyizt'=>1]);

                            Db::name('jiaoyi_uinfo')->where('uname',$value['uname'])->setInc($value['bizhong'],$value['jiaoyinum']);

                            Db::commit();

                            echo json_encode(['zt'=>1,'msg'=>'检查到账状态完成']);

                        } catch (\Exception $e) {

                            echo json_encode(['zt'=>0,'msg'=>'检查到账状态失败']);

                            Db::rollback();
                        }

                    }elseif ($value['jiaoyitype'] == 1){//转出减少

                        Db::startTrans();

                        try{

                            Db::name('jiaoyiqianbaolog')->where('hash',$value['hash'])->update(['jiaoyizt'=>1]);

                            Db::name('jiaoyi_uinfo')->where('uname',$value['uname'])->setDec('d'.$value['bizhong'],$value['jiaoyinum']);

                            Db::commit();

                            echo json_encode(['zt'=>1,'msg'=>'检查到账状态完成']);

                        } catch (\Exception $e) {

                            echo json_encode(['zt'=>0,'msg'=>'检查到账状态失败']);

                            Db::rollback();

                        }

                    }else{

                        if (Db::name('jiaoyiqianbaolog')->where('hash',$value['hash'])->update(['jiaoyizt'=>0])){

                            echo json_encode(['zt'=>0,'msg'=>'检查到账状态失败-状态成功至0']);

                        }else{

                            echo json_encode(['zt'=>0,'msg'=>'检查到账状态失败']);

                        }

                    }

                }else{

                    echo json_encode(['zt'=>0,'msg'=>'没有查到该哈希']);
                }

            }else{

                echo json_encode(['zt'=>0,'msg'=>'没有查到符合条件的订单']);

            }

        }

    }

    //交易所到钱包
    public function Jiaoyiqianbao(){

        $res = Request::instance();

        $uname = $this->Jiemi($res->param('uname'));

        $pass = $this->Jiemi($res->param('pass'));

        $type = $this->Jiemi($res->param('type'));

        $value = $res->param('value');

        //echo json_encode(['zt'=>1,'msg'=>$uname]);die();

        $user_address = Db::name('adeth')->where('uname',$uname)->value('address');

        $user_pass = Db::name('user')->where('uname',$uname)->value('secpwd');

        $yuer = Db::name('jiaoyi_uinfo')->where('uname',$uname)->value($type);

        if (floatval($yuer)<$value+0.0005){echo json_encode(['zt'=>0,'msg'=>'余额不足,账户最少要留0.0005以太坊']);die();}

        if ($value < 0.1){echo json_encode(['zt'=>0,'msg'=>'转出最低限额为0.1不包含手续费']);die();}

        if (Db::name('jiaoyizhuanchudj')->where('uname',$uname)->where('zt',0)->find()){echo json_encode(['zt'=>0,'msg'=>'您有未结算的申请,请结算后在申请！']);die();}

        //echo json_encode(['zt'=>1,'msg'=>$nonce+1]);die();

        if (md5($pass) == $user_pass){

            if (strtolower($type) == 'eth'){

                Db::startTrans();

                try{

                    Db::name('jiaoyiqianbaolog')->insert([

                        'uname' => $uname,

                        //'hash' => json_decode($zhuan)->hash,

                        'jiaoyizt' => 3,

                        'jiaoyinum' => $value,

                        'jiaoyiinfo' => '场外转出',

                        'jiaoyitype' => 1,//转出

                        'from' => $this->zongqianbao,

                        'to' => $user_address,

                        'bizhong' => strtolower($type),

                        'time' => time(),

                        //'nonce' => json_decode($zhuan)->nonce

                    ]);

                    Db::name('jiaoyizhuanchudj')->insert([

                        'uname' => $uname,

                        'djnum' => $value,

                        'time' => time(),

                        'djnote' => '提现冻结',

                        'to' => $user_address,

                        'formaddr' => $this->zongqianbao,

                        'zt' => 0

                    ]);

                    Db::name('jiaoyi_uinfo')->where('uname',$uname)->setDec(strtolower($type),$value);

                    Db::name('jiaoyi_uinfo')->where('uname',$uname)->setInc('d'.strtolower($type),$value);

                    Db::commit();

                    echo json_encode(['zt'=>1,'msg'=>'提交成功,等待系统打包']);

                } catch (\Exception $e) {

                    echo json_encode(['zt'=>0,'msg'=>'充值失败,稍后重试']);

                    Db::rollback();
                }


            }else{

                echo json_encode(['zt'=>0,'msg'=>'暂时只支持以太坊']);die();

                $daibizhuan = file_get_contents('http://'.$this->host_ip.':8885/?type=2&from='.$this->zongqianbao.'&privateKey='.$this->zongqianbaokey.'&to='.$user_address.'&value='.$value.'&contract='.Config::get('con_' . strtolower($type)));

                //echo $daibizhuan;

                echo json_encode(['zt'=>1,'msg'=>$daibizhuan]);

            }

        }else{

            echo json_encode(['zt'=>0,'msg'=>'支付密码错误']);

        }



    }

    //数据解密
    private function Jiemi($data){

        $jiami_data = openssl_decrypt(base64_decode($data,false),"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV'));

        return $jiami_data;

    }
    //数据加密
    private function Jiami($data){

        $jiemi_data =  base64_encode(openssl_encrypt($data,"AES-128-CBC",Config::get('Key'),OPENSSL_RAW_DATA,Config::get('IV')));

        return $jiemi_data;

    }


}